<?php

 
class CV_Testimonial_Widget extends WP_Widget {
	
	var $prefix;
	var $textdomain;
	
	/**
	 * Set up the widget's unique name, ID, class, description, and other options.
	 * @since 0.7
	 */
	function CV_Testimonial_Widget() {
		$this->prefix = 'cv';
		$this->textdomain = hybrid_get_textdomain();	
		
		$widget_ops = array('classname' => 'widget-testimonial', 'description' => __('Testimonial.', $this->textdomain) );
		$this->WP_Widget("{$this->prefix}-testimonial-widget", __('Testimonial', $this->textdomain), $widget_ops); 		
	}
	
	/**
	 * Outputs the widget based on the arguments input through the widget controls.
	 */
	function widget($args, $instance) {  
		extract( $args );
		
		echo $before_widget;

		/* If there is a title given, add it along with the $before_title and $after_title variables. */
		$title = $instance['title'];
		if ( $title) {
			$title =  apply_filters( 'widget_title',  $title, $instance, $this->id_base );
			$title = str_replace('testimonial', '<span>testimonial<span>r</span></span>', $title);
			echo $before_title . $title . $after_title;
		}	
				
		$name = empty($instance['name'])? ' ':' name="'. $instance['name'] . '"';
		$sitename = empty($instance['sitename'])? ' ':' sitename="'. $instance['sitename'] . '"';
		$siteurl = empty($instance['siteurl'])? ' ':' siteurl="'. $instance['siteurl'] . '"';
		$avatar = empty($instance['avatar'])? ' ':' avatar="'. $instance['avatar'] . '"';
		$content = $instance['content'];
		
		$shortcode = '[quote' . $name . $sitename . $siteurl . $avatar . ']' . $content . '[/quote]'; //name="colorvila" sitename="ColorVila" siteurl="http://www.colorvila.com" avatar="sfdsfd"]Quote content[/quote]
		 ?>	
		<div class="testimonial-info">
			<?php echo do_shortcode(stripslashes($shortcode));  ?>	
		</div>	
		
		<?php
		
	   echo $after_widget;
   }
	
	/**
	 * Updates the widget control options for the particular instance of the widget.
	 * @since 0.7
	 */
	function update($new_instance, $old_instance) {                
       return $new_instance;
   }
	
	/**
	 * Displays the widget control options in the Widgets admin screen.	
	 */
	function form($instance) {  
		$defaults = array(
			'title' => 'Testimonial',			
			'name' => '',			
			'sitename' => '',
			'siteurl' => '',
			'avatar' => '',
			'content' => ''
		);
		$instance = wp_parse_args( (array) $instance, $defaults ); 		
		?>
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', $this->textdomain ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e( 'Name:', $this->textdomain ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" value="<?php echo esc_attr( $instance['name'] ); ?>" />
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id( 'sitename' ); ?>"><?php _e( 'Sitename:', $this->textdomain ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'sitename' ); ?>" name="<?php echo $this->get_field_name( 'sitename' ); ?>" value="<?php echo esc_attr( $instance['sitename'] ); ?>" />
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id( 'siteurl' ); ?>"><?php _e( 'Siteurl:', $this->textdomain ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'siteurl' ); ?>" name="<?php echo $this->get_field_name( 'siteurl' ); ?>" value="<?php echo esc_attr( $instance['siteurl'] ); ?>" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'avatar' ); ?>"><?php _e( 'Avatar:', $this->textdomain ); ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id( 'avatar' ); ?>" name="<?php echo $this->get_field_name( 'avatar' ); ?>" value="<?php echo esc_attr( $instance['avatar'] ); ?>" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'content' ); ?>"><?php _e( 'Content:', $this->textdomain ); ?></label>
		
			<textarea rows="6" class="widefat" id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>"><?php echo esc_attr( $instance['content'] ); ?></textarea>
		</p>		
		
       <?php
	}

}
 
add_action('widgets_init', 'register_cv_testimonial_widget');

function register_cv_testimonial_widget() {
	register_widget('CV_Testimonial_Widget');
}